// pages/me/periodsetting.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: new Date().getFullYear() + '-' + new Date().getMonth() + 1 + '-' + new Date().getDate(),
    endTime: new Date().getFullYear() + '-' + new Date().getMonth() + 1 + '-' + new Date().getDate(),
    day: [],
    cycle: [],
    law: ['早来', '正常', '晚来'],
    index1: 3,
    index2: 13,
    index3: 1,
    isFirst: false,            //判断是不是第一次进来设置经期
    isSub: false                //是否显示提交按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var arrDay = [];
    var arrCycle = [];
    for (let i = 2; i <= 14; i++) {
      arrDay.push(i);
    }
    for (let j = 15; j <= 40; j++) {
      arrCycle.push(j);
    }
    this.setData({
      day: arrDay,
      cycle: arrCycle
    });
    // 判断是不是第一次设置经期
    if (options['fisrt']) {
      this.setData({
        isFirst: true,
        isSub: true
      });
    } else {
      this.getperiod();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDayChange: function (e) {
    console.log(e);
    this.setData({
      index1: e.detail.value,
      isSub: true
    });
  },
  bindCycleChange: function (e) {
    console.log(e);
    this.setData({
      index2: e.detail.value,
      isSub: true
    });
  },
  bindLawChange: function (e) {
    console.log(e);
    this.setData({
      index3: e.detail.value,
      isSub: true
    });
  },
  bindDateChange: function (e) {
    console.log(e);
    this.setData({
      date: e.detail.value,
      isSub: true
    });
  },
  // 保存/设置经期设置
  settingSub: function () {
    console.log('保存');
    var that = this;
    var date = this.data.date;
    var day = this.data.day[this.data.index1];
    var cycle = this.data.cycle[this.data.index2];
    var law = this.data.index3;
    console.log(date, day, cycle, law);
    wx.request({
      url: app.globalData.serverUrl + 'setPeriod',
      method: 'get',
      data: {
        openid: app.globalData.openid,
        name: app.globalData.userInfo.nickName,
        before: date,
        day: day,
        cycle: cycle,
        law: law,
        isown: 0
      }, success: function (e) {
        console.log(e);
        if ((e.data.status == 1)) {
          console.log(that.data.isFirst);
          if (that.data.isFirst) {
            wx.showToast({
              title: '设置成功',
              icon: 'success',
              duration: 2000,
              success: function (res) {
                console.log(res);
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/record/index',
                    success: function (e) {
                      // console.log(e);
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                      page.isPeriod();
                    }
                  })
                }, 2000);
              }
            })
          } else {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000,
              success: function (res) {
                // console.log(res);
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/me/index',
                    success: function (e) {
                      // console.log(e);
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                      page.onLoad();
                    }
                  })
                }, 2000);
              }
            })
          }
        } else {
          wx.showToast({
            title: e.content,
            icon: 'loading',
            duration: 2000,
            success: function (res) {
              // console.log(res);
              // setTimeout(function () {
              //   wx.hideToast();
              // }, 2000);
            }
          })
        }
      }
    })
  },
  // 获取经期设置的数据
  getperiod: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getPeriod',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        console.log(e);
        var day = 0;
        var cycle = 0;
        console.log(that.data.day)
        for (var i = 0; i < that.data.day.length; i++) {
          if (that.data.day[i] == e.data.day) {
            day = i;
            break;
          }
        }
        for (var j = 0; j < that.data.cycle.length; j++) {
          if (that.data.cycle[j] == e.data.cycle) {
            cycle = j;
            break;
          }
        }
        console.log(day, cycle);
        that.setData({
          index1: day,
          index2: cycle,
          index3: e.data.law
        });
      }
    })
  }
})